package com.tool;


import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

public class Sms {

    private static String host = "http://rcsapi.wo.cn:8000/umcinterface/sendtempletmsg";

    public static int main(String tel_num,String num) {

        JSONObject json = new JSONObject();
        String cpcode = "AABEEF";
        String mobiles = tel_num;
        String msg = "";
        String excode = "";
        String templetid = num;
        String key = "d5c3dc2a3f7dd350746291a848cfd045";
        //d5c3dc2a3f7dd350746291a848cfd045
        //f4930b8cb3a5e3293fcd209dc3a706f5
        String md5source = cpcode + msg + mobiles + excode + templetid + key;
        String print = cpcode + " " + msg + " "+ mobiles+ " " +excode + " " + templetid + " " + key;
        String sign = "";
        try {
            sign = makeMD5(new String(md5source.getBytes("UTF-8"), "UTF-8")).toLowerCase();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        json.put("cpcode", cpcode);
        json.put("msg", msg);
        json.put("excode", excode);
        json.put("mobiles", mobiles);
        json.put("templetid", templetid);
        json.put("sign", sign);
//        System.out.println("---sign---" + sign);
//        System.out.println(post(json.toString()));
        String result = postTest(json.toString());
        Map map = JSON.parseObject(result);
        return Integer.parseInt(map.get("resultcode").toString());
    }



    private static String postTest(String data){
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod("http://rcsapi.wo.cn:8000/umcinterface/sendtempletmsg");
        post.addRequestHeader("Content-Type", "application/json");// 在头文件中设置转码
        post.setRequestBody(data);

        try {
            client.executeMethod(post);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();

        String result = null;
        try {
            result = new String(post.getResponseBodyAsString().getBytes("utf-8"));


        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonString = Objects.requireNonNull(JSON.toJSON(result)).toString();
        post.releaseConnection();
        return jsonString;
    }


    public static String makeMD5(String plainText) {

        String re_md5 = new String();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes("UTF-8"));
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            re_md5 = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return re_md5;
    }



}
